﻿using WebFormsMvp.Contrib.Presenters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using WebFormsMvp.Contrib.Views;
using WebFormsMvp.Contrib.Data.Services;
using Rhino.Mocks;
using WebFormsMvp.Contrib.Data.Models;
using System.Collections.Generic;
using System.Linq;

namespace WebFormsMvp.Contrib.Tests
{
    
    
    /// <summary>
    ///This is a test class for NewsArchivePresenterTest and is intended
    ///to contain all NewsArchivePresenterTest Unit Tests
    ///</summary>
    [TestClass()]
    public class NewsArchivePresenterTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod]
        public void DataSerivce_Accessed()
        {
            //Arrange
            var view = MockRepository.GenerateMock<INewsArchiveView>();
            view.Expect(v => v.Model).Return(new Models.NewsArchiveModel());
            var dataService = MockRepository.GenerateMock<INewsService>();
            dataService.Expect(d => d.GetNewsArticles()).Return(new List<INewsArticle>()
            {
                MockRepository.GenerateMock<INewsArticle>(),
                MockRepository.GenerateMock<INewsArticle>(),
                MockRepository.GenerateMock<INewsArticle>(),
                MockRepository.GenerateMock<INewsArticle>()
            });
            var presenter = new NewsArchivePresenter(view, dataService);

            //Act
            view.Raise(v => v.Load += null, this, EventArgs.Empty);
            presenter.ReleaseView();

            //Assert
            Assert.IsNotNull(view.Model.NewsArticles);
            dataService.VerifyAllExpectations();
        }

        [TestMethod]
        public void View_Model_Count_Based_On_View()
        {
            //Arrange
            var view = MockRepository.GenerateMock<INewsArchiveView>();
            view.Expect(v => v.Model).Return(new Models.NewsArchiveModel());
            view.Expect(v => v.NumberOfArticles).Return(1);
            var dataService = MockRepository.GenerateMock<INewsService>();
            dataService.Expect(d => d.GetNewsArticles()).Return(new List<INewsArticle>()
            {
                MockRepository.GenerateMock<INewsArticle>(),
                MockRepository.GenerateMock<INewsArticle>(),
                MockRepository.GenerateMock<INewsArticle>(),
                MockRepository.GenerateMock<INewsArticle>()
            });
            var presenter = new NewsArchivePresenter(view, dataService);

            //Act
            view.Raise(v => v.Load += null, this, EventArgs.Empty);
            presenter.ReleaseView();

            //Assert
            Assert.AreEqual<int>(view.NumberOfArticles, view.Model.NewsArticles.Count(), "Number of articles should match that specified in the view");
            view.VerifyAllExpectations();
            dataService.VerifyAllExpectations();
        }
    }
}
